package com.qjz.web.servlet;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import com.qjz.util.FileOperate;

/**
 * 
 * @author crystal
 */
public class FileUploaded extends HttpServlet {

	private static final String CONTENT_TYPE = "text/html;charset=UTF-8";

	private static final String HEAD_ENCODING = "UTF-8";

	public void init() throws ServletException {
		super.init();
	}

	/**
	 * 
	 * <code>GET</code> 和 <code>POST</code>的共同上传方法
	 * 
	 * @param request
	 *            servlet request
	 * 
	 * @param response
	 *            servlet response
	 */
	public void processRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		PrintWriter out = response.getWriter();

		response.setContentType(CONTENT_TYPE);

		String uploadDir = FileOperate.getDir("upload");

		String fName = request.getParameter("fName");

		// 设置文件最大可上传大小
		String size = "4";
		int maxSize = Integer.parseInt(size) * 1024 * 1024;
		
		String newPath = uploadDir + "\\upload\\";

		DiskFileItemFactory factory = new DiskFileItemFactory();
		ServletFileUpload upload = new ServletFileUpload(factory);
		upload.setHeaderEncoding(HEAD_ENCODING);
		// 设置最大可上传大小
		try {
			upload.setSizeMax(maxSize);
			List fileItems = upload.parseRequest(request);
			for (Object fileItem : fileItems) {
				FileItem item = (FileItem) fileItem;
				if (!item.isFormField()) {
					String fullName = item.getName();
					if (null == fullName || "".equals(fullName)) {
						continue;
					}
					String fileType = fullName.substring(fullName
							.lastIndexOf("."));
					String saveName = System.currentTimeMillis() + fileType;
					if(null != fName)
						saveName = fName + fileType;

					File uploadPathFile = new File(newPath);
					if (!uploadPathFile.exists()) {
						uploadPathFile.mkdirs();
					}
					File saveFile = new File(uploadPathFile, saveName);

					item.write(saveFile);
					out.write(newPath + saveName);
				}
			}
		} catch (FileUploadException fileUploadException) {
			fileUploadException.printStackTrace();
			out.println(fileUploadException.getMessage());
		} catch (IOException ioException) {
			ioException.printStackTrace();
			out.println(ioException.getMessage());
		} catch (NullPointerException exception) {
			exception.printStackTrace();
			out.println(exception.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
			out.write(e.getMessage());
		}
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response)

	throws ServletException, IOException {

		processRequest(request, response);

	}

	/** */
	/**
	 * 
	 * Handles the HTTP <code>POST</code> method.
	 * 
	 * @param request
	 *            servlet request
	 * 
	 * @param response
	 *            servlet response
	 */

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response)

	throws ServletException, IOException {

		processRequest(request, response);

	}

	/**
	 * Returns a short description of the servlet.
	 * 
	 * @return String
	 */
	public String getServletInfo() {

		return "Short description";

	}
}